﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace SecureCookie.Implementation.UnitTest.Stub
{
    class StubbedHttpContext :  IDisposable
    {
        public StubbedHttpContext()
        {
            HttpContext.Current = new HttpContext(new StubbedWorker());
        }

        public HttpCookieCollection RequestCookies
        {
            get
            {
                return HttpContext.Current.Request.Cookies;
            }
        }
        public HttpCookieCollection ResponseCookies
        {
            get
            {
                return HttpContext.Current.Response.Cookies;
            }
        }

        public void ServerSideProcess(Action serverSideAction)
        {
            using (var module = new SecureCookieHttpModule())
            {
                module.BeginRequest(null, null);
                serverSideAction();
                module.EndRequest(null, null);
            }
        }

        public void Dispose()
        {
            HttpContext.Current = null;
        }

        class StubbedWorker : HttpWorkerRequest
        {

            public override void EndOfRequest()
            {
            }

            public override void FlushResponse(bool finalFlush)
            {
            }

            public override string GetHttpVerbName()
            {
                return "GET";
            }

            public override string GetHttpVersion()
            {
                return "1";
            }

            public override string GetLocalAddress()
            {
                return "127.0.0.1";
            }

            public override int GetLocalPort()
            {
                return 80;
            }

            public override string GetQueryString()
            {
                return "";
            }

            public override string GetRawUrl()
            {
                return "http://localhost/test";
            }

            public override string GetRemoteAddress()
            {
                return "192.168.1.1";
            }

            public override int GetRemotePort()
            {
                return 80;
            }

            public override string GetUriPath()
            {
                return "/test";
            }

            public override void SendKnownResponseHeader(int index, string value)
            {
            }

            public override void SendResponseFromFile(IntPtr handle, long offset, long length)
            {
            }

            public override void SendResponseFromFile(string filename, long offset, long length)
            {
            }

            public override void SendResponseFromMemory(byte[] data, int length)
            {
            }

            public override void SendStatus(int statusCode, string statusDescription)
            {
            }

            public override void SendUnknownResponseHeader(string name, string value)
            {
            }
        }
    }
}
